package com.fancy.mobileschool.ui.adapt;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.fancy.mobileschool.R;
import java.io.File;
import java.util.List;

/**
 * Created by Jackie on 2017/11/14.
 */

public class SDFileListAdapter extends RecyclerView.Adapter<SDFileListAdapter.SDFile> {

    private List<File> files;

    private Context context;

    private OnItemClickListener listener;

    public SDFileListAdapter(List<File> files, Context context, OnItemClickListener listener) {
        this.files = files;
        this.listener = listener;
        this.context = context;
    }

    @Override
    public SDFile onCreateViewHolder(ViewGroup parent, int viewType) {
        View view =  LayoutInflater.from(context).inflate(R.layout.sd_file, parent, false);
        return new SDFile(view);
    }

    @Override
    public void onBindViewHolder(SDFile holder, int position) {
        if (files.get(position).isDirectory()) {
            holder.fileIcon.setImageResource(R.drawable.dir);
            holder.view.setId(R.id.sd_dir);
        }
        else {
            holder.fileIcon.setImageResource(R.drawable.file_icon);
            holder.view.setId(R.id.sd_file);
        }
        holder.fileName.setText(files.get(position).getName());
        holder.view.setOnClickListener(holder);
    }


    @Override
    public int getItemCount() {
        return files.size();
    }



    class SDFile extends RecyclerView.ViewHolder implements View.OnClickListener{

        private View view;

        private ImageView fileIcon;

        private TextView fileName;


        public SDFile(View itemView) {
            super(itemView);
            this.view = itemView;
            fileIcon = (ImageView) itemView.findViewById(R.id.file_icon);
            fileName = (TextView) itemView.findViewById(R.id.file_name);
        }

        @Override
        public void onClick(View view) {
            listener.onItemClick(view, getAdapterPosition());
        }
    }

    public void refreshData(List<File> files) {
        this.files = files;
        notifyDataSetChanged();
    }

    public static interface OnItemClickListener {

        void onItemClick(View view, int position);

    }


}
